package com.token.sysflow.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.token.base.Pl;
import com.token.sql.IntegrateSql;
import com.token.sql.TableBeanPrepareSql;
import com.token.sysapp.bean.FSysAppInfo;
import com.token.sysflow.bean.FlowFlowInfo;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author chenjianxiong 2020/4/28
 */
public class AppFlowTree {
    TableBeanPrepareSql tSql;
    FlowFlowInfo flowInfo;
    JSONArray flowType;
    String selectedKeys;

    /**
     * @param f
     * @throws Exception
     */
    public AppFlowTree(IntegrateSql f) throws Exception {
        tSql = new TableBeanPrepareSql(new FSysAppInfo(f));
        flowInfo = new FlowFlowInfo(f);
        selectedKeys = "";
    }

    public String getSelectedKeys(){
        return selectedKeys;
    }
    /**
     * tree 的结构,左边树的数据，单个应用--流程-版本
     *
     * @return
     */
    public JSONArray getAppFlowTreeData(String appKeyId) throws Exception {
        JSONArray array = new JSONArray();
        JSONArray children;
        tSql.setSelectPrepare("SysId=? and appKeyId=?");
        tSql.setPstValue(1, Pl.getSysId());
        tSql.setPstValue(2, appKeyId);
        ResultSet rs = tSql.executeQuery();
        if (rs.next()) {
            JSONObject json;
            do {
                json = new JSONObject();
                children = new JSONArray();
                json.put("title", rs.getString("AppDesc"));
                json.put("key", rs.getString("AppKeyId"));
                json.put("selectable", false);
                json.put("children", children);
                array.add(json);
                readFlowByAppKeyId(children, appKeyId);
            } while (rs.next());
        }
        rs.close();
        tSql.close();

        return array;
    }

    /**
     * @param array
     * @param appKeyId
     * @throws Exception
     */
    private void readFlowByAppKeyId(JSONArray array, String appKeyId) throws Exception {
        tSql.setPrepare("select IsActive,IsDefault,FlowName,FlowVersion,FlowId from " + flowInfo.getTableName() + " where SysId=? and AppKeyId=? order by FlowVersion");
        tSql.setPstValue(1, Pl.getSysId());
        tSql.setPstValue(2, appKeyId);
        ResultSet rs = tSql.executeQuery();
        if (rs.next()) {
            JSONObject json;
            do {
                json = new JSONObject();

                json.put("title",(rs.getString("IsDefault").equals("1")?"√ ":"") + rs.getString("FlowName")+ ' ' + rs.getString("FlowVersion"));
                json.put("key", rs.getString("FlowId"));
                json.put("isActive", rs.getInt("IsActive"));
                json.put("isLeaf", true);

                if(rs.getString("IsDefault").equals("1")){
                    selectedKeys = rs.getString("FlowId");
                }
                array.add(json);
            } while (rs.next());
        }
        rs.close();
        tSql.close();
    }

    /**
     *
     */
    public void close() {
        flowType = null;
        try {
            flowInfo.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        tSql.close();
    }
}
